﻿Imports CRM.BusinessLayer
Imports System.ComponentModel
Imports System.Data
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Shapes
Imports System.Windows.Controls.Primitives
Imports Microsoft.Windows.Controls
Imports Microsoft.Windows.Controls.Primitives
Imports System.Collections.ObjectModel

Partial Public Class frmAdminTransferCases

    Private _dtData As System.Data.DataTable
    Public Property dtData() As System.Data.DataTable
        Get
            Return _dtData
        End Get
        Set(ByVal value As System.Data.DataTable)
            _dtData = value
        End Set
    End Property

    Private Sub frmAdminTransferCases_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles Me.KeyUp
        If e.Key = Key.Escape Then
            Me.Close()
        End If
    End Sub


    Private Sub frmAdminTransferCases_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)
        '.GetEnumerator
        With Me.drpBranch
            .SelectedValuePath = "Branch_ID"
            .DisplayMemberPath = "Branch_Name"
            .ItemsSource = CType(CommonDataFunctions.BranchList, IListSource).GetList
            .SelectedValue = GlobalCRM.CurrentBranchId
        End With
        AddHandler drpBranch.SelectionChanged, AddressOf drpBranch_SelectionChanged
        With Me.drpDepts
            .SelectedValuePath = "Department_ID"
            .DisplayMemberPath = "Department_Name"
            .ItemsSource = CType(CommonDataFunctions.DepartmentList(Me.drpBranch.SelectedValue), IListSource).GetList
            .SelectedIndex = 0
        End With
        AddHandler drpDepts.SelectionChanged, AddressOf drpDepts_SelectionChanged
        'RemoveHandler drpCons.SelectionChanged, AddressOf drpCons_SelectionChanged
        With Me.drpCons
            .SelectedValuePath = "con_id"
            .DisplayMemberPath = "con_name"
            .ItemsSource = CType(CommonDataFunctions.ConsultantList(True, drpDepts.SelectedValue), IListSource).GetList
        End With
        AddHandler drpCons.SelectionChanged, AddressOf drpCons_SelectionChanged
    End Sub


    Private Sub drpBranch_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs)

        Try
            With Me.drpDepts
                .SelectedValuePath = "Department_ID"
                .SelectedValuePath = "Department_Name"
                .ItemsSource = CType(CommonDataFunctions.DepartmentList(Me.drpBranch.SelectedValue), IListSource).GetList
                .SelectedIndex = -1
                drpDepts_SelectionChanged(sender, e)
            End With
        Catch ex As Exception
            ' CommonFuncs.ErrorHandler(ex, "frmMainForm.drpBranch_SelectedIndexChanged", False)
        End Try
    End Sub

    Private Sub drpCons_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs)
        Dim dt As DataTable
        Try

            'DataBinding.BindListView(Admin.TestDummyList, Me.lstCases)
            'Me.DataContext = Admin.TestDummyList
            'dtData = Admin.GetCasesFromConsultant(drpCons.SelectedValue, Admin.ConsultantRole.CSM)
            dt = Admin.GetCasesFromConsultant(drpCons.SelectedValue, Admin.ConsultantRole.CSM)
            'DataBinding.BindListView(dt, Me.lstCases)
            Dim items As New ObservableCollection(Of String)()
            'items.Add("Item 1")
            'items.Add("Item 2")
            'items.Add("Item 3")
            'items.Add("Item 4")
            'items.Add("Item 5")
            For i = 0 To dt.Rows.Count - 1
                items.Add(dt.Rows(i).ItemArray(0) & " : " & dt.Rows(i).ItemArray(1) & " : " & dt.Rows(i).ItemArray(2))
            Next
            DataContext = items
            'If dt.Rows.Count > 0 Then
            '    grpTo.IsEnabled = True
            'Else
            '    grpTo.IsEnabled = False
            'End If
        Catch ex As Exception

            CommonFuncs.ErrorHandler(ex, "frmMainForm.drpCons.SelectedIndexChanged", False)
        Finally
            dt = Nothing
        End Try
    End Sub

    Private Sub drpDepts_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs)
        Try
            RemoveHandler drpCons.SelectionChanged, AddressOf drpCons_SelectionChanged

            With Me.drpCons
                .ItemsSource = Nothing
                .Items.Clear()
                .SelectedValuePath = "con_id"
                .DisplayMemberPath = "con_name"
                .ItemsSource = CType(CommonDataFunctions.ConsultantList(True, Me.drpDepts.SelectedValue, , Me.drpBranch.SelectedValue), IListSource).GetList
                .SelectedIndex = -1
            End With

            If drpDepts.Items.Count = 0 Then
                drpCons.ItemsSource = Nothing
                drpCons.Items.Clear()
            End If

            AddHandler drpCons.SelectionChanged, AddressOf drpCons_SelectionChanged
        Catch ex As Exception
        End Try
    End Sub

    Private Sub btnTransfer_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Dim items As New StringBuilder()
        items.AppendFormat("Items selected count: {0}", checkedListView.SelectedItems.Count).AppendLine()
        For Each item As String In checkedListView.SelectedItems
            items.AppendLine(item)
        Next
        MessageBox.Show(items.ToString())
    End Sub

    Private Sub chkSelectAll_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)

    End Sub

    Private Sub chkSelectAll_Unchecked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)

        Dim lstvItem As ListViewItem = CType(lstcases.Items(1), IListSource).GetList

        'If (((System.Windows.Controls.ListViewItem)(lstcases.Items(1) as ListViewItem)).Content as CheckBox).IsChecked=true then
        'End If
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Me.Close()

    End Sub
    Private Sub OnShowSelectedItems(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim items As New StringBuilder()
        items.AppendFormat("Items selected count: {0}", checkedListView.SelectedItems.Count).AppendLine()
        For Each item As String In checkedListView.SelectedItems
            items.AppendLine(item)
        Next
        MessageBox.Show(items.ToString())
    End Sub

    Private Sub OnUncheckItem(ByVal sender As Object, ByVal e As RoutedEventArgs)
        selectAll.IsChecked = False
    End Sub

    Private Sub OnSelectAllChanged(ByVal sender As Object, ByVal e As RoutedEventArgs)
        If selectAll.IsChecked.HasValue AndAlso selectAll.IsChecked.Value Then
            checkedListView.SelectAll()
        Else
            checkedListView.UnselectAll()
        End If
    End Sub

End Class
